import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    public int lengthOfLongestSubstring (String s) {
        // write code here

        Map<Character, Integer> map = new HashMap<>();

        int[] len = new int[s.length()];
        int ret = 0;

        for (int i = 0; i < s.length(); i++) {
            Character key = s.charAt(i);
            if (map.containsKey(key)) {
                int index = map.get(key);
                // !!
                len[i] = Math.min(i - index, len[i - 1] + 1);
            }
            else {
                len[i] = (i == 0 ? 1 : len[i - 1] + 1);
            }
            ret = Math.max(ret, len[i]);
            map.put(key, i);
        }

        return ret;
    }
}